package com.proposition.web.paper.export;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.proposition.commons.model.*;
import com.proposition.exam.service.ExamAcupointAdultService;
import com.proposition.exam.service.ExamAnswer10Service;
import com.proposition.paper.service.PaperExamService;
import com.proposition.paper.service.PaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author 杨凯
 * @create 2018-05-21 14:57
 **/
@Slf4j
@Controller
@RequestMapping("/export/excel")
public class ExportExcelController {


	@Autowired
	private PaperService paperService;

	@Autowired
	private PaperExamService paperExamService;

	@Autowired
	private ExamAcupointAdultService examAcupointAdultService;

	@Autowired
	private ExamAnswer10Service examAnswer10Service;


	/**
	 * 导出试卷和答案
	 * 通过试卷Id 导出试卷
	 *
	 * @param response
	 * @param paperIds 试卷Id
	 */
	@GetMapping(value = "/acupoint")
	public void exportKeGuanHuaExcel(String[] paperIds, HttpServletResponse response) {
		try {
			List<String> title = CollUtil.newArrayList("考试批次", "考试年度", "命题套数", "考试命题", "第几套命题", "命题类型", "命题人");
			List<List<String>> rows = CollUtil.newArrayList();
			rows.add(title);
			for (int i = 0; i < paperIds.length; i++) {
				PaperExam paperExam = paperExamService.selectPaperExamByPaperId(paperIds[i]);
				Paper paper = paperService.selectById(paperIds[i]);
				String[] exam1 = paperExam.getExam1().split(",");
				StringBuffer sb = new StringBuffer();
				for (String id : exam1) {
					ExamAcupointAdult examAcupointAdult = examAcupointAdultService.selectExamAcupointAdultById(id);
					sb.append(examAcupointAdult.getStem() + " ");
				}
				List<String> row = CollUtil.newArrayList("1", "2018", String.valueOf(paperIds.length), sb.toString(), String.valueOf(i + 1), paper.getType().getDesc(), paper.getUsername());
				rows.add(row);
			}
			//通过工具类创建writer
			//通过构造方法创建writer
			ExcelWriter writer = new ExcelWriter();
			response.reset();
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			String fileName = new String("实践技能模拟考题（穴位）".getBytes("GB2312"), "ISO-8859-1") + ".xls";
			response.addHeader("Content-disposition", "attachment; filename=" + fileName);
			OutputStream os = response.getOutputStream();
			writer.write(rows);
			writer.flush(os);
			writer.close();
		} catch (NullPointerException e) {
			log.warn("试卷导出Excel----》传入试卷Id参数无效");
		} catch (Exception e) {
			log.error("试卷导出Excel===》未知异常[{}]", e.getMessage());
		}
	}

	@GetMapping("/technique")
	public void exportTechnique(String[] paperIds, HttpServletResponse response) throws IOException {
		List<String> title = CollUtil.newArrayList("考试批次", "考试年度", "命题套数", "手法类型", "手法子类型", "第几套命题", "命题类型", "命题人");
		List<List<String>> rows = CollUtil.newArrayList();
		rows.add(title);
		for (int i = 0; i < paperIds.length; i++) {
			PaperExam paperExam = paperExamService.selectPaperExamByPaperId(paperIds[i]);
			Paper paper = paperService.selectById(paperIds[i]);
			String[] exam2 = paperExam.getExam2().split(",");
			for (String id : exam2) {
				ExamAnswer10 examAnswer10 = examAnswer10Service.selectById(id);
				ExamSinglePloyFiveCategory singlePloyFiveCategory = new ExamSinglePloyFiveCategory();
				String category = singlePloyFiveCategory.selectById(examAnswer10.getSinglePloyFiveCategoryId()).getName();
				List<String> row = CollUtil.newArrayList("1", "2018", String.valueOf(paperIds.length), category, examAnswer10.getStem(), String.valueOf(i + 1), paper.getType().getDesc(), paper.getUsername());
				rows.add(row);
			}
		}
		//通过工具类创建writer
		//通过构造方法创建writer
		ExcelWriter writer = new ExcelWriter();
		response.reset();
		response.setContentType("application/octet-stream");
		response.addHeader("Content-Disposition", "attachment; filename=" + new String("实践技能模拟考题（手法）".getBytes("GB2312"), "ISO-8859-1") + ".xls");
		OutputStream outputStream = response.getOutputStream();
		writer.write(rows);
		writer.flush(outputStream);
		writer.close();

	}

}
